

#include "data_global.h"
#include "sem.h"
#include "mpu6050.h"

#define N 1024

//共享内存
extern int shmid;
extern key_t shm_key;

//信号量
extern int semid;
extern key_t sem_key;

// 串口设备文件描述符
extern int ttyUSB0fd;
extern int mpu6050fd;

int file_env_info_a9_zigbee_stm32(struct env_info_client_addr *rt_status,int home_id);

//刷新上传共享内存数据线程 （刷新共享内存环境数据供CGI进程使用）
void *pthread_refresh(void *arg)
{
	struct shm_addr *shm_buf;

	//创建信号量
	if((sem_key = ftok("/tmp",'i')) < 0){
		perror("ftok failed .\n");
		exit(-1);
	}
	//独占创建信号量
	semid = semget(sem_key,1,IPC_CREAT|IPC_EXCL|0666);
	if(semid == -1)	{
		if(errno == EEXIST){
			semid = semget(sem_key,1,0777);
		}else{
			perror("fail to semget");
			exit(1);
		}
	}else{
		init_sem(semid, 0, 1);
	}

	//创建共享内存
	if((shm_key = ftok("/tmp",'i')) < 0){
		perror("ftok failed .\n");
		exit(-1);
	}
	//独占创建共享内存
	shmid = shmget(shm_key,N,IPC_CREAT|IPC_EXCL|0666);
	if(shmid == -1)	{
		if(errno == EEXIST){
			shmid = shmget(shm_key,N,0777);
		}else{
			perror("fail to shmget");
			exit(1);
		}
	}
	//得到共享内存
	if((shm_buf = (struct shm_addr *)shmat(shmid,NULL,0)) == (void *)-1)
	{
		perror("fail to shmat");
		exit(1);
	}
	
	bzero (shm_buf, sizeof (struct shm_addr));

	while(1){
		//P操作
		sem_p(semid,0);
		printf("pthread_refresh ......\n");
		shm_buf->shm_status = 1;
		//获得ZIgbe上送的温湿度数据、MPU6050上送的加速度数据
		file_env_info_a9_zigbee_stm32(&shm_buf->sm_all_env_info, shm_buf->shm_status);
		sleep(2);
		//V操作
		sem_v(semid,0);
	}
}


int file_env_info_a9_zigbee_stm32(struct env_info_client_addr *rt_status,int home_id)
{
	int  env_info_size = sizeof(struct env_info_client_addr);

	//获取Zigbee里的温湿度数据
	char cmdbuf[4] = {0};
	strcpy(cmdbuf, "22\n");
	write(ttyUSB0fd, cmdbuf, sizeof(cmdbuf) / sizeof(cmdbuf[0]));
	//读取Zigbee返回的数据
	char buf[32] = {0};
	read(ttyUSB0fd,&buf,sizeof(buf));
	printf("buf = %s.\n", buf);
	float temp = 0, humi = 0;
	sscanf(buf, "%f  %f", &temp, &humi);	

	rt_status->monitor_no[home_id].zigbee_info.head[0]  = 'm';
	rt_status->monitor_no[home_id].zigbee_info.head[1]  = 's';
	rt_status->monitor_no[home_id].zigbee_info.head[2]  = 'm';
	rt_status->monitor_no[home_id].zigbee_info.temperature = temp;
	rt_status->monitor_no[home_id].zigbee_info.tempMIN = 2.0;
	rt_status->monitor_no[home_id].zigbee_info.tempMAX = 20.0;
	rt_status->monitor_no[home_id].zigbee_info.humidity  = humi;
	rt_status->monitor_no[home_id].zigbee_info.humidityMIN  = 10.0;
	rt_status->monitor_no[home_id].zigbee_info.humidityMAX  = 30.0;
	rt_status->monitor_no[home_id].zigbee_info.reserved[0]  = 0.01;
	rt_status->monitor_no[home_id].zigbee_info.reserved[1]  = -0.01;

	//获取A9的MPU6050芯片上的信息
	union mpu6050_data data = {{0}};
	ioctl(mpu6050fd, GET_ACCEL, &data);
	printf("ACCEL X is 0x%x\n", data.accel.x);
	printf("ACCEL Y is 0x%x\n", data.accel.y);
	printf("ACCEL Z is 0x%x\n", data.accel.z);
	
	ioctl(mpu6050fd, GET_GYRO, &data);
	printf("GYRO X is 0x%x\n", data.gyro.x);
	printf("GYRO Y is 0x%x\n", data.gyro.y);
	printf("GYRO Z is 0x%x\n", data.gyro.z);

	rt_status->monitor_no[home_id].a9_info.head[0]  = 'm';
	rt_status->monitor_no[home_id].a9_info.head[1]  = 's';
	rt_status->monitor_no[home_id].a9_info.head[2]  = 'm';

	rt_status->monitor_no[home_id].a9_info.gyrox  =  (short)data.gyro.x;
	rt_status->monitor_no[home_id].a9_info.gyroy  =  (short)data.gyro.y;
	rt_status->monitor_no[home_id].a9_info.gyroz  =  (short)data.gyro.z;

	rt_status->monitor_no[home_id].a9_info.aacx   =  (short)data.accel.x;
	rt_status->monitor_no[home_id].a9_info.aacy   =  (short)data.accel.y;
	rt_status->monitor_no[home_id].a9_info.aacz   =  (short)data.accel.z;
	rt_status->monitor_no[home_id].a9_info.reserved[0]  = 0.01;
	rt_status->monitor_no[home_id].a9_info.reserved[1]  = -0.01;

	return 0;
}
